package com.design.yjx.observer.jdkbuitlin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

/**
 * @author yuanjianxin
 * @date 2017/8/2 16:51
 * 当前天气情况显示面板实现类
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {
    //温度
    private float temperature;
    //湿度
    private float humidity;
    //气压
    private float pressure;

    //更新时间
    private Date date;

    private Observable observable;


    //创建对象的时候将观察者注册到主题
    public CurrentConditionsDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    //模拟展示，打印出参数结果即可
    @Override
    public void display() {
        System.out.println("当前天气情况显示面板---温度:" + temperature + "℃,湿度:" + humidity + "%,气压:" + pressure + "Pa,更新时间："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
    }


    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData) {
            WeatherData weatherData = (WeatherData) o;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            this.pressure = weatherData.getPressure();
            if (arg instanceof Date) {
                this.date = (Date) arg;
            }

            display();
        }
    }
}
